#include "sg4c.h"
#include <ctype.h>
#include <string.h>

sg_font_t font;
char typebuf[40];
int chcount;
void setup(){
    font = sgCreateFont("Arial", 24);
    chcount=0;
}

void draw(){
    //clear screen
    sgNoStroke();
    sgFill(sgGray(255));
    sgRect(0, 0, sgWidth(), sgHeight());

    //draw text
    if( chcount > 0 ){
        sgFill(sgGray(0x30));
        sgFont(font);
        sgText(typebuf, 0, font.height);
    }
}


//events
void mouseMoved(){
    //Mouse moved
}

void mousePressed(){
    //A mouse button is pressed
}

void mouseReleased(){
    //A mouse button is released
}

void mouseClicked(){
    //A mouse button is pressed and released
}

void mouseDragged(){
    //The mouse moves when being pressed
}

void mouseEntered(){
    //The mouse pointer enters the display frame
}

void mouseExited(){
    //The mouse pointer leaves the display frame
}

void keyReleased(){
    //A key is released

}

void keyPressed(){
    //A key is pressed
}

void keyTyped(){
    char ch = sgKey().character;
    if( (isalnum(ch) || ch==' ') && chcount<sizeof(typebuf)-1 ){
        typebuf[chcount] = ch;
        chcount++;
        typebuf[chcount] = '\0';
        sgRedraw();
    }

}

int main(){
    sg_config_t config;
    sgInitConfig( &config );

    config.screen.width = 400;  //set the horizontal resolution of the frame
    config.screen.height = 100; //set the vertical resolution of the frame
    config.framerate = 0;      //set the per-second framerate


    config.smooth = 1; // uncomment this line to enable smoothing

    //------------------------------------------------------------------
    //DO NOT MODIFY CODE BELOW

    //setup callbacks
    config.setup = setup;
    config.draw = draw;

    config.mousePressed = mousePressed;
    config.mouseReleased = mouseReleased;
    config.mouseMoved = mouseMoved;
    config.mouseEntered = mouseEntered;
    config.mouseExited = mouseExited;
    config.mouseClicked = mouseClicked;
    config.mouseDragged = mouseDragged;

    config.keyReleased = keyReleased;
    config.keyPressed = keyPressed;
    config.keyTyped = keyTyped;

    //initilize the system
    sgSetup(&config);

    //run forever
    return sgRunForever();
}
